-
Notifications
You must be signed in to change notification settings - Fork 22
/
Ship.scala
182 lines (168 loc) · 7.16 KB
/
Ship.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package models
import scalikejdbc.SQLInterpolation._
import scalikejdbc.{WrappedResultSet, DBSession}
import com.ponkotuy.data
import util.scalikejdbc.BulkInsert._
import dat.ShipWithName
/**
*
* @author ponkotuy
* Date: 14/02/26.
*/
case class Ship(
id: Int, shipId: Int, memberId: Long,
lv: Int, exp: Int, nowhp: Int, slot: List[Int], fuel: Int, bull: Int, dockTime: Long, cond: Int,
karyoku: Int, raisou: Int, taiku: Int, soukou: Int, kaihi: Int, taisen: Int, sakuteki: Int, lucky: Int, locked: Boolean,
created: Long, maxhp: Int)
object Ship extends SQLSyntaxSupport[Ship] {
override val columnNames = Seq("id", "ship_id", "member_id", "lv", "exp", "nowhp", "fuel", "bull", "dock_time", "cond",
"karyoku", "raisou", "taiku", "soukou", "kaihi", "taisen", "sakuteki", "lucky", "locked", "created", "maxhp")
def apply(x: SyntaxProvider[Ship], slot: List[Int])(rs: WrappedResultSet): Ship =
apply(x.resultName, slot)(rs)
def apply(x: ResultName[Ship], slot: List[Int])(rs: WrappedResultSet): Ship = {
new Ship(
rs.int(x.id),
rs.int(x.shipId),
rs.long(x.memberId),
rs.int(x.lv),
rs.int(x.exp),
rs.int(x.nowhp),
slot,
rs.int(x.fuel),
rs.int(x.bull),
rs.long(x.dockTime),
rs.int(x.cond),
rs.int(x.karyoku),
rs.int(x.raisou),
rs.int(x.taiku),
rs.int(x.soukou),
rs.int(x.kaihi),
rs.int(x.taisen),
rs.int(x.sakuteki),
rs.int(x.lucky),
rs.boolean(x.locked),
rs.long(x.created),
rs.int(x.maxhp)
)
}
lazy val s = Ship.syntax("s")
lazy val ms = MasterShipBase.syntax("ms")
lazy val ds = DeckShip.syntax("ds")
lazy val ssi = ShipSlotItem.syntax("ssi")
lazy val mst = MasterStype.syntax("mst")
def findByUserMaxLvWithName(memberId: Long)(implicit session: DBSession = autoSession): Option[ShipWithName] = {
withSQL {
select.from(Ship as s)
.leftJoin(MasterShipBase as ms).on(s.shipId, ms.id)
.leftJoin(MasterStype as mst).on(ms.stype, mst.id)
.where.eq(s.memberId, memberId)
.and.eq(s.lv, sqls"(SELECT MAX(${s.lv}) FROM ${Ship.table} s WHERE ${s.memberId} = ${memberId})")
}.map { rs =>
val slot = findSlot(memberId, rs.int(s.resultName.id))
ShipWithName(Ship(s, slot)(rs), MasterShipBase(ms)(rs), MasterStype(mst)(rs))
}.first().apply()
}
def findByIDWithName(memberId: Long, sid: Int)(
implicit session: DBSession = autoSession): Option[ShipWithName] = {
withSQL {
select.from(Ship as s)
.leftJoin(MasterShipBase as ms).on(s.shipId, ms.id)
.leftJoin(MasterStype as mst).on(ms.stype, mst.id)
.where.eq(s.memberId, memberId).and.eq(s.id, sid)
}.map { rs =>
val slot = findSlot(memberId, rs.int(s.resultName.id))
ShipWithName(Ship(s, slot)(rs), MasterShipBase(ms)(rs), MasterStype(mst)(rs))
}.first().apply()
}
def findIn(memberId: Long, ids: Seq[Int])(implicit session: DBSession = autoSession): List[ShipWithName] =
ids match {
case Seq() => Nil
case _ =>
val slots = ShipSlotItem.findAllInShip(memberId, ids)
withSQL {
select.from(Ship as s)
.leftJoin(MasterShipBase as ms).on(s.shipId, ms.id)
.leftJoin(MasterStype as mst).on(ms.stype, mst.id)
.where.eq(s.memberId, memberId).and.in(s.id, ids)
}.map { rs =>
val id = rs.int(s.resultName.id)
val slot = slots.filter(_.shipId == id).map(_.slotitemId)
ShipWithName(Ship(s, slot)(rs), MasterShipBase(ms)(rs), MasterStype(mst)(rs))
}.toList().apply()
}
def findAllByUser(memberId: Long)(implicit session: DBSession = Ship.autoSession): List[Ship] = {
val slots = ShipSlotItem.findAllBy(sqls"member_id = ${memberId}")
withSQL {
select.from(Ship as s)
.where.eq(s.memberId, memberId)
}.map { rs =>
val id = rs.int(s.resultName.id)
val slot = slots.filter(_.shipId == id).map(_.slotitemId)
Ship(s, slot)(rs)
}.toList().apply()
}
def findAllByUserWithName(memberId: Long)(implicit session: DBSession = Ship.autoSession): List[ShipWithName] = {
val slots = ShipSlotItem.findAllBy(sqls"member_id = ${memberId}")
withSQL {
select.from(Ship as s)
.leftJoin(MasterShipBase as ms).on(s.shipId, ms.id)
.leftJoin(MasterStype as mst).on(ms.stype, mst.id)
.where.eq(s.memberId, memberId)
}.map { rs =>
val id = rs.int(s.resultName.id)
val slot = slots.filter(_.shipId == id).map(_.slotitemId)
ShipWithName(Ship(s, slot)(rs), MasterShipBase(ms)(rs), MasterStype(mst)(rs))
}.toList().apply()
}
def findSlot(memberId: Long, shipId: Int): List[Int] =
ShipSlotItem.findAllBy(sqls"member_id = ${memberId} and ship_id = ${shipId}").map(_.slotitemId)
def create(s: data.Ship, memberId: Long)(implicit session: DBSession = Ship.autoSession): Unit = {
val created = System.currentTimeMillis()
ShipSlotItem.bulkInsert(s.slot, memberId, s.id)
applyUpdate {
insert.into(Ship).namedValues(
column.id -> s.id, column.shipId -> s.shipId, column.memberId -> memberId,
column.lv -> s.lv, column.exp -> s.exp, column.nowhp -> s.nowhp,
column.dockTime -> s.dockTime, column.cond -> s.cond,
column.karyoku -> s.karyoku, column.raisou -> s.raisou, column.taiku -> s.taiku, column.soukou -> s.soukou,
column.kaihi -> s.kaihi, column.taisen -> s.taisen, column.sakuteki -> s.sakuteki, column.lucky -> s.lucky,
column.locked -> s.locked, column.created -> created,
column.maxhp -> s.maxhp
)
}
}
def bulkInsert(ss: Seq[data.Ship], memberId: Long)(
implicit session: DBSession = Ship.autoSession): Unit = {
val created = System.currentTimeMillis()
ShipSlotItem.bulkInserts(ss.map(_.slot), memberId, ss.map(_.id))
applyUpdate {
insert.into(Ship).columns(
column.id, column.shipId, column.memberId,
column.lv, column.exp, column.nowhp,
column.fuel, column.bull, column.dockTime, column.cond,
column.karyoku, column.raisou, column.taiku, column.soukou,
column.kaihi, column.taisen, column.sakuteki, column.lucky,
column.locked, column.created, column.maxhp
).multiValues(
ss.map(_.id), ss.map(_.shipId), Seq.fill(ss.size)(memberId),
ss.map(_.lv), ss.map(_.exp), ss.map(_.nowhp),
ss.map(_.fuel), ss.map(_.bull), ss.map(_.dockTime), ss.map(_.cond),
ss.map(_.karyoku), ss.map(_.raisou), ss.map(_.taiku), ss.map(_.soukou),
ss.map(_.kaihi), ss.map(_.taisen), ss.map(_.sakuteki), ss.map(_.lucky),
ss.map(_.locked), Seq.fill(ss.size)(created),
ss.map(_.maxhp)
)
}
}
def destroy(memberId: Long, id: Int)(implicit session: DBSession = autoSession): Unit = {
ShipSlotItem.deleteAllBy(sqls"member_id = ${memberId} and ship_id = ${s.id}")
applyUpdate {
delete.from(Ship)
.where.eq(Ship.column.memberId, memberId).and.eq(Ship.column.id, id)
}
}
def deleteAllByUser(memberId: Long)(implicit session: DBSession = Ship.autoSession): Unit = {
ShipSlotItem.deleteAllBy(sqls"member_id = ${memberId}")
applyUpdate { delete.from(Ship).where.eq(Ship.column.memberId, memberId) }
}
}